#include <iostream>
#include <unordered_set>

using namespace std;
struct ListNode
{
    int val;
    struct ListNode* next;
    ListNode(int x) :
        val(x), next(nullptr) 
    {}
};
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
    unordered_set<ListNode*> s;
    while (pHead)
    {
        if (s.count(pHead))
        {
            return pHead;
        }
        s.insert(pHead);
        pHead = pHead->next;
    }
    return pHead;
}